package com.web.controller.student;

import com.common.apiresult.CommonResult;
import com.common.domain.dto.AddGuestRecordDTO;
import com.common.domain.dto.GuestRecordQueryDTO;
import com.service.serviceInterface.GuestService;
import com.service.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * @author liuxuhuang
 * @date 2023年04月04日 11:23
 * @desc
 */
@RequestMapping(value = "/student/guest")
@RestController(value = "studentGuestController")
@Api(value = "guestController", tags = "访客信息控制器")
@Slf4j
public class GuestController {
    @Resource
    private SecurityUtil securityUtil;
    @Resource
    private GuestService guestService;

    @PutMapping
    @ApiOperation(value = "修改访客记录,只能修改未来的访客记录")
    public CommonResult update(@RequestBody @Validated AddGuestRecordDTO dto) {
        long userId = securityUtil.getUserId(SecurityContextHolder.getContext());
        return guestService.updateData(dto, userId);
    }

    @DeleteMapping
    @ApiOperation(value = "删除访客记录,只能删除未来的访客记录，不能删除过去的访客记录")
    public CommonResult delete(@RequestParam("id") @NotBlank(message = "参数不能为空") String id) {
        return guestService.delete(id);
    }

    @PostMapping
    @ApiOperation(value = "新增访客记录")
    public CommonResult add(@RequestBody @Validated AddGuestRecordDTO dto) {
        long userId = securityUtil.getUserId(SecurityContextHolder.getContext());
        return guestService.add(dto, userId);
    }

    @GetMapping
    @ApiOperation(value = "获取访客记录，只能获取自己填的记录")
    public CommonResult get(GuestRecordQueryDTO dto) {
        long userId = securityUtil.getUserId(SecurityContextHolder.getContext());
        return guestService.getData(dto, userId);
    }
}
